package com.sgb.kjwl.viewmodel.power;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.PowerAchievedEntity;
import com.sgb.kjwl.model.entity.PowerDetailEntity;
import com.sgb.kjwl.model.entity.PowerTalentEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.PowerRepertory;
import com.sgb.kjwl.view.ui.activity.power.PowerActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PowerDetailModel extends BaseViewModel {

    private final PowerActivity activity;
    private final PowerRepertory repertory;
    private int achievedPage = 1;
    private int talentPage = 1;

    public PowerDetailModel(PowerActivity activity, PowerRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getDetail(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getPowerAPI().getDetail(id).enqueue(new Callback<BaseEntity<PowerDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PowerDetailEntity>> call, Response<BaseEntity<PowerDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showDetail(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PowerDetailEntity>> call, Throwable t) {

            }
        });
    }

    public void getAchieved(String id, boolean isLoadMore) {
        if (isLoadMore) {
            achievedPage = achievedPage + 1;
        } else {
            achievedPage = 1;
        }

        APIManager.getInstance().getPowerAPI().getPerformanceByCompanyNo(id, achievedPage, 10).enqueue(new Callback<BaseEntity<ListResponse<List<PowerAchievedEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PowerAchievedEntity>>>> call, Response<BaseEntity<ListResponse<List<PowerAchievedEntity>>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (isLoadMore) {
                        activity.showMoreAchieved(response.body().getData().getList());
                    } else {
                        activity.showAchieved(response.body().getData().getList());
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PowerAchievedEntity>>>> call, Throwable t) {

            }
        });
    }


    public void getTalent(String id, boolean isLoadMore) {
        if (isLoadMore) {
            talentPage = talentPage + 1;
        } else {
            talentPage = 1;
        }

        APIManager.getInstance().getPowerAPI().getTalentsByCompanyNo(id, talentPage, 10).enqueue(new Callback<BaseEntity<ListResponse<List<PowerTalentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PowerTalentEntity>>>> call, Response<BaseEntity<ListResponse<List<PowerTalentEntity>>>> response) {
                if (response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (isLoadMore) {
                        activity.showMoreTalent(response.body().getData().getList());
                    } else {
                        activity.showTalent(response.body().getData().getList());
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PowerTalentEntity>>>> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
                String message = t.getMessage();
            }
        });
    }
}
